package systems.mocap
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageDisplayState;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.geom.ColorTransform;
	import flash.geom.Point;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import systems.particles.Renderer3D;
	
	/**
	 * @author Nicolas Barradeau
	 * http://en.nicoptere.net
	 */
	public class Mocap extends Sprite 
	{
		private var lines:Array;
		private var header:Array;
		private var positions:Array;
		private var lineLength:int;
		private var i:int, j:int;
		private var SW:int;
		private var SH:int;
		private var step:int;
		private var bd:BitmapData;
		private var canvas:Shape = new Shape();
		public function Mocap() 
		{
			bd = new BitmapData( 1366, 768, false, 0 );
			addChild( new Bitmap( bd ) );
			//addChild( canvas )
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.displayState = StageDisplayState.FULL_SCREEN;
			
			
			Renderer3D.focal = 10000
			var req:URLRequest = new URLRequest( '../lib/mocap/mocap7.txt' );
			var ul:URLLoader = new URLLoader( req );
			ul.addEventListener( Event.COMPLETE, completeHandler );

			SW = stage.stageWidth / 2;
			SH = stage.stageHeight / 2;
			step = 2;

		}

		private function completeHandler( e:Event ):void
		{

			lines = e.target.data.split("\n");
			header = lines[ 0 ].split( '	' );
			lineLength = header.length;
			
			var i:int = 0;
			for ( i = 2; i < lineLength; i+=3 )
			{
			}
			addEventListener( Event.ENTER_FRAME, enterFrameHandler );
			
		}


		private function enterFrameHandler (e:Event ):void
		{
			update();
			update();
		}
		private function update():void
		{
		
			step++
			step %= lines.length-1;
			
			var i:int = 0;
			positions = lines[ step ].split( '	' );
			
			canvas.graphics.clear();
			canvas.graphics.lineStyle( 0,0xFFFFFF, .1 );
			
			
			var X:int = SW - int( positions[ 0 ] ) / 10;
			var Y:int = SH - int( positions[ 2 ] ) / 10;
			var scale:Number = .3;
			var vertices:Vector.<Number> = new Vector.<Number>();
			for ( i = 2; i < lineLength - 3; i+=3 )
			{
				vertices.push( positions[ i ] *scale, -positions[ i + 2 ] *scale, positions[ i + 1 ] *scale );
			}
			
			var projection:Vector.<Number> = Renderer3D.project( Renderer3D.transform( vertices ), new Point(), true );
			
			for ( i = 0; i < projection.length; i+=2 )
			{
				//drawCross( SW + projection[i], SH * 1.5 + projection[ i + 1 ], 5 );
				canvas.graphics.drawCircle( bd.width / 2 + projection[i], SH * 2- 100 + projection[ i + 1 ], 1 );
				
			}
			//if( step%4==0 )bd.colorTransform( bd.rect, new ColorTransform( 1, 1, 1, .9999 ) );
			bd.draw( canvas );
			//C7:X	C7:Y	C7:Z	
			//
			//CLAV:X	CLAV:Y	CLAV:Z	2
			//LANK:X	LANK:Y	LANK:Z	4
			
			//LBHD:X	LBHD:Y	LBHD:Z	6
			//LBWT:X	LBWT:Y	LBWT:Z	8
			//LELB:X	LELB:Y	LELB:Z	10
			//LFHD:X	LFHD:Y	LFHD:Z	12
			//LFIN:X	LFIN:Y	LFIN:Z	14
			//LFRM:X	LFRM:Y	LFRM:Z	16
			//LFWT:X	LFWT:Y	LFWT:Z	18
			//
			//graphics.lineStyle( 2, 0xFFFF00 );
			//
			//var id:int;
			//var leftArm:Array = [ 2, 6, 8, 10 ];// , 12, 14, 16, 18 ];
			//graphics.moveTo(  SW + projection[ leftArm[ 0 ] ],  SH * 1.5 + projection[leftArm[ 0 ]+1 ] );
			//for ( i = 0; i < leftArm.length; i++ )
			//{
				//id = leftArm[ i ]
				//graphics.lineTo(   SW + projection[ id ],  SH * 1.5 + projection[ id + 1 ] );
			//}
			//
			//LHEE:X	LHEE:Y	LHEE:Z	
			//LKNE:X	LKNE:Y	LKNE:Z	
			//LMT5:X	LMT5:Y	LMT5:Z	
			//LSHN:X	LSHN:Y	LSHN:Z	
			//LSHO:X	LSHO:Y	LSHO:Z	
			//LTHI:X	LTHI:Y	LTHI:Z	
			//LTOE:X	LTOE:Y	LTOE:Z	
			//
			//LUPA:X	LUPA:Y	LUPA:Z	
			//LWRA:X	LWRA:Y	LWRA:Z	
			//LWRB:X	LWRB:Y	LWRB:Z	
			//
			//
			//RANK:X	RANK:Y	RANK:Z	
			//RBAC:X	RBAC:Y	RBAC:Z	
			//RBHD:X	RBHD:Y	RBHD:Z	
			//RBWT:X	RBWT:Y	RBWT:Z	
			//RELB:X	RELB:Y	RELB:Z	
			//RFHD:X	RFHD:Y	RFHD:Z	
			//RFRM:X	RFRM:Y	RFRM:Z	
			//RFWT:X	RFWT:Y	RFWT:Z	
			//RHEE:X	RHEE:Y	RHEE:Z	
			//RKNE:X	RKNE:Y	RKNE:Z	
			//RMT5:X	RMT5:Y	RMT5:Z	
			//RSHN:X	RSHN:Y	RSHN:Z	
			//RSHO:X	RSHO:Y	RSHO:Z	
			//RTHI:X	RTHI:Y	RTHI:Z	
			//RTOE:X	RTOE:Y	RTOE:Z	
			//
			//RUPA:X	RUPA:Y	RUPA:Z	
			//RWRA:X	RWRA:Y	RWRA:Z	
			//RWRB:X	RWRB:Y	RWRB:Z	
			//STRN:X	STRN:Y	STRN:Z	
			//
			//T10:X	T10:Y	T10:Z
			
			
		}
		
		private function drawCross( x:Number, y:Number, size:Number = 5):void 
		{
			canvas.graphics.moveTo( x - size, y );
			canvas.graphics.lineTo( x + size, y );
			
			canvas.graphics.moveTo( x, y - size );
			canvas.graphics.lineTo( x, y + size );
		}

	}

}